<template>
  <div class="new">
    <h1 style="font-size: 20px; margin-left: 20px; color: #0000ff">新建文章</h1>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="文章标题" prop="title"
        ><el-input v-model="ruleForm.title"></el-input
      ></el-form-item>

      <el-form-item label="文章描述" prop="description"
        ><el-input v-model="ruleForm.description"></el-input
      ></el-form-item>

      <el-form-item label="文章封面" prop="poster"
        ><el-input v-model="ruleForm.poster"></el-input
      ></el-form-item>

      <el-form-item label="文章类型" prop="type"
        ><el-input v-model="ruleForm.type"></el-input
      ></el-form-item>

      <el-form-item label="文章作者" prop="author"
        ><el-input v-model="ruleForm.author"></el-input
      ></el-form-item>

      <el-form-item label="文章详情" prop="detail">
        <quill-editor
          v-model="ruleForm.detail"
          ref="myQuillEditor"
          :options="editorOption"
          @blur="onEditorBlur($event)"
          @focus="onEditorFocus($event)"
          @ready="onEditorReady($event)"
        >
        </quill-editor>
        <el-input
          type="textarea"
          v-model="ruleForm.detail"
          cols="7"
          rows="9"
          style="outline: none"
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      ruleForm: {
        title: "",
        description: "",
        detail: "",
        poster: "",
        type: "",
        author: "",
      },

      rules: {
        title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入文章描述", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        detail: [
          { required: true, message: "请输入文章详情", trigger: "blur" },
        ],
        type: [{ required: true, message: "请输入文章类型", trigger: "blur" }],
        poster: [
          { required: true, message: "请输入封面地址", trigger: "blur" },
          { type: "url", message: "请按地址格式输入", trigger: "blur" },
        ],
        author: [
          { required: true, message: "请输入文章作者", trigger: "blur" },
        ],
      },
      content: "<h2>I am Example</h2>",
      editorOption: {
        theme: "snow",
        palceholder: "请在这里输入请在这里输入",
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
          console.log(this.ruleForm);
          // ajax
          axios({
            url: "http://120.27.209.174:1010/api/v1/posts",
            method: "post",
            data: this.resetForm,
          })
            .then(function (response) {
              console.log(response);
            })
            .catch(function (error) {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      console.log(formName);
      // this.$refs[formName].resetFields();
    },
    onEditorBlur(editor) {
      console.log("editor blur!", editor);
    },
    onEditorFocus(editor) {
      console.log("editor focus!", editor);
    },
    onEditorReady(editor) {
      console.log("editor ready!", editor);
    },
  },
};
</script>

<style></style>
